﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;

namespace FillWordExcelTemplate
{
    public class Helper
    {
        public static string getCellStringValueAllCase(NPOI.SS.UserModel.ICell tCell)
        {
            string tempValue = "";
            switch (tCell.CellType)
            {
                case NPOI.SS.UserModel.CellType.Blank:
                    break;
                case NPOI.SS.UserModel.CellType.Boolean:
                    tempValue = tCell.BooleanCellValue.ToString();
                    break;
                case NPOI.SS.UserModel.CellType.Error:
                    break;
                case NPOI.SS.UserModel.CellType.Formula:
                    NPOI.SS.UserModel.IFormulaEvaluator fe = NPOI.SS.UserModel.WorkbookFactory.CreateFormulaEvaluator(tCell.Sheet.Workbook);
                    var cellValue = fe.Evaluate(tCell);
                    switch (cellValue.CellType)
                    {
                        case NPOI.SS.UserModel.CellType.Blank:
                            break;
                        case NPOI.SS.UserModel.CellType.Boolean:
                            tempValue = cellValue.BooleanValue.ToString();
                            break;
                        case NPOI.SS.UserModel.CellType.Error:
                            break;
                        case NPOI.SS.UserModel.CellType.Formula:
                            break;
                        case NPOI.SS.UserModel.CellType.Numeric:
                            tempValue = cellValue.NumberValue.ToString();
                            break;
                        case NPOI.SS.UserModel.CellType.String:
                            tempValue = cellValue.StringValue.ToString();
                            break;
                        case NPOI.SS.UserModel.CellType.Unknown:
                            break;
                        default:
                            break;
                    }
                    break;
                case NPOI.SS.UserModel.CellType.Numeric:

                    if (NPOI.SS.UserModel.DateUtil.IsCellDateFormatted(tCell))
                    {
                        tempValue = tCell.DateCellValue.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        tempValue = tCell.NumericCellValue.ToString();
                    }
                    break;
                case NPOI.SS.UserModel.CellType.String:
                    tempValue = tCell.StringCellValue.Trim();
                    break;
                case NPOI.SS.UserModel.CellType.Unknown:
                    break;
                default:
                    break;
            }
            return tempValue;
        }
        /// <summary>
        /// 从txt文件中得到项目名称，再显示到comboBox上
        /// </summary>
        public static void BindComboFromTxt(System.Windows.Forms.ComboBox comboBox)
        {
            string ReadLine;
            string[] array;
            string Path = AppDomain.CurrentDomain.BaseDirectory + "ComboList.TXT";
            StreamReader reader = new StreamReader(Path, System.Text.Encoding.GetEncoding("GB2312"));
            List<MyItem> list = new List<MyItem>();
            while (reader.Peek() >= 0)
            {
                try
                {
                    ReadLine = reader.ReadLine();
                    if (ReadLine != "")
                    {
                        ReadLine = ReadLine.Replace("\"", "");
                        array = ReadLine.Split(',');
                        if (array.Length == 0)
                        {
                            MessageBox.Show("您选择的导入数据类型有误，请重试！");
                            return;
                        }
                        // 列表集合将作为comboBox1的数据源                     
                        list.Add(new MyItem(array[0], array[1]));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            // 绑定
            comboBox.DataSource = list;
            // 在comboBox1中显示MyItem的Name属性
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "Value";
        }
        /// <summary>
        /// 从txt文件中得到替换的字典数据
        /// </summary>
        /// <param name="IsExcelPercent">是否为Excel模板</param>
        public static void BindDictWord(Dictionary<string, string> DicWord, bool IsExcelPercent)
        {
            DicWord.Clear();
            string ReadLine;
            string[] array;
            string Path = AppDomain.CurrentDomain.BaseDirectory + "DictWord.TXT";
            StreamReader reader = new StreamReader(Path, System.Text.Encoding.GetEncoding("GB2312"));
            List<MyItem> list = new List<MyItem>();
            while (reader.Peek() >= 0)
            {
                try
                {
                    ReadLine = reader.ReadLine();
                    if (ReadLine != "")
                    {
                        ReadLine = ReadLine.Replace("\"", "");
                        array = ReadLine.Split(',');
                        if (array.Length == 0)
                        {
                            MessageBox.Show("您选择的导入数据类型有误，请重试！");
                            return;
                        }
                        string name = array[0].ToLower();
                        string value = array[1].ToLower();
                        if (IsExcelPercent == true)
                        {
                            //同环比中，兼容excel中百分比：除以100
                            if (name.EndsWith("_tb}") || name.EndsWith("_hb}"))//替换字符以_tb}或_hb}结束
                            {
                                if (value == "")
                                { value = "0"; }
                                value = (double.Parse(value) / 100).ToString();
                            }
                        }
                        DicWord.Add(name, value);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        private class MyItem
        {
            public MyItem(string name, string value)
            {
                Name = name;
                Value = value;
            }
            public string Name { get; private set; }
            public string Value { get; private set; }
        }
    }
}
